package com.zde.moriserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zde.moriserver.entity.FriendEntity;
import com.zde.moriserver.entity.FriendEntityRepository;
import com.zde.moriserver.entity.UsersEntity;
import com.zde.moriserver.entity.UsersEntityRepository;
import com.zde.moriserver.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.mpe.bind.Binder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 获取用户信息
 */
@RestController
@RequestMapping("/users")
@Tag(name = "用户信息")
public class UserInfoController {

    @Autowired
    private UsersEntityRepository usersEntityRepository;
    // 好友列表
    @Autowired
    private FriendEntityRepository friendEntityRepository;

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //获取用户的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        System.out.println("loginIdAsInt = " + loginIdAsInt);
        UsersEntity usersEntity = usersEntityRepository.getById(loginIdAsInt);
        return Result.success(usersEntity);
    }

    //添加好友
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody Map map){
        String nickName = (String) map.get("nickName");
        //1 判断是否有这个用户
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        qw.eq("nickname",nickName);
        UsersEntity one = usersEntityRepository.getOne(qw);
        // 判断
        if(one == null){
            return Result.fail("没有这个用户");
        }else{
            //自己不能加自己
            if(one.getId().equals(StpUtil.getLoginIdAsInt())){
                return Result.fail("不能添加自己");
            }


            //2 判断是否已经是好友
            QueryWrapper<FriendEntity> qw1 = new QueryWrapper<>();
            //token获取的自己的id
            qw1.eq("user_id",StpUtil.getLoginIdAsInt());
            qw1.eq("friend_id",one.getId());
            FriendEntity one1 = friendEntityRepository.getOne(qw1);
            if(one1 != null){
                return Result.fail("已经是好友或已发送请求");
            }else{
                //3 发生添加好友的请求
                FriendEntity friendEntity = new FriendEntity();
                friendEntity.setUserId(StpUtil.getLoginIdAsString());
                friendEntity.setFriendId(one.getId());
                //
                friendEntity.setStatus(1);
                friendEntityRepository.save(friendEntity);
                return Result.success("发送成功");
            }
        }
    }

    // 获取待添加的好友列表 Status为0的
    @GetMapping("/getFriendList")
    public Result getFriendList(){
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("friend_id",StpUtil.getLoginIdAsInt());
        qw.eq("status",1);
        List<FriendEntity> list = friendEntityRepository.list(qw);
        //循环，通过好友userId查询用户信息给usersEntity
        for (FriendEntity friendEntity : list) {
            QueryWrapper<UsersEntity> qw1 = new QueryWrapper<>();
            qw1.eq("id",friendEntity.getUserId());
            UsersEntity one = usersEntityRepository.getOne(qw1);
            friendEntity.setUsersEntity(one);
        }
        return Result.success(list);
    }

    //修改用户性别
    @PutMapping("/updateSex")
    public Result updateSex(@RequestBody Map map){
        Integer sex = (Integer) map.get("sex");
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setId(StpUtil.getLoginIdAsString());
        // 判断0是男 1是女
        if(sex==0){
            usersEntity.setSex("男");
        }else{
            usersEntity.setSex("女");
        }
        usersEntityRepository.updateById(usersEntity);
        return Result.success("修改成功");
    }

    //同意好有请求
    @PutMapping("/agreeFriend")
    public Result agreeFriend(@RequestBody Map map){
        String userId = (String) map.get("userId");
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("friend_id",StpUtil.getLoginIdAsInt());
        qw.eq("status",1);
        FriendEntity one = friendEntityRepository.getOne(qw);
        if(one != null){
            one.setStatus(2);
            friendEntityRepository.updateById(one);

            //在我的列表里面也要有他
            FriendEntity friendEntity = new FriendEntity();
            friendEntity.setUserId(StpUtil.getLoginIdAsString());
            friendEntity.setFriendId(userId);
            friendEntity.setStatus(2);
            friendEntityRepository.save(friendEntity);

            return Result.success("修改成功");
        }else{
            return Result.fail("没有这个请求");
        }
    }

    //拒绝，删除好友添加的请求
    @DeleteMapping("/deleteFriend")
    public Result deleteFriend(@RequestBody Map map){
        String userId = (String) map.get("userId");
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("friend_id",StpUtil.getLoginIdAsInt());
        FriendEntity one = friendEntityRepository.getOne(qw);
        if(one != null){
            friendEntityRepository.removeById(one.getId());
        }
        return Result.success("删除成功");
    }

    //  获取自己的好友列表
    @GetMapping("/getMyFriendList")
    public Result getMyFriendList(){
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("user_id",StpUtil.getLoginIdAsInt());
        qw.eq("status",2);
        List<FriendEntity> list = friendEntityRepository.list(qw);
        //循环，通过好友userId查询用户信息给usersEntity
        for (FriendEntity friendEntity : list) {
            QueryWrapper<UsersEntity> qw1 = new QueryWrapper<>();
            qw1.eq("id",friendEntity.getFriendId());
            UsersEntity one = usersEntityRepository.getOne(qw1);
            friendEntity.setUsersEntity(one);
        }
        return Result.success(list);
    }

}
